/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.router.core.repository.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import info.peigen.hotpot.business.router.core.domain.RouterTransLog;
import info.peigen.hotpot.business.router.core.domain.action.RouterTransLogConverter;
import info.peigen.hotpot.business.router.core.repository.entity.RouterTransLogEntity;
import info.peigen.hotpot.business.router.core.repository.mapper.RouterTransLogMapper;
import info.peigen.hotpot.business.router.facade.enums.RouterAction;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import org.noear.solon.annotation.Component;

import java.util.List;

/**
 * <b>(RouterTransLogRepository)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/8/5
 */
@Component
public class RouterTransLogRepository extends AbstractBeetlSqlRepository<RouterTransLogMapper, RouterTransLogEntity> {

    public RouterTransLog active(String transLogId) {
        RouterTransLogEntity entity = findByTransLogId(transLogId);
        if (ObjectUtil.isNull(entity)) {
            return null;
        }
        return RouterTransLogConverter.convert(entity);
    }

    public List<RouterTransLog> active(List<String> transLogIds) {
        List<RouterTransLogEntity> entities = mapper.createLambdaQuery().andIn("trans_log_id", transLogIds).select();
        if (ObjectUtil.isEmpty(entities)) {
            return null;
        }
        return RouterTransLogConverter.convertBatch(entities);
    }

    public void store(RouterTransLog transLog) {
        RouterTransLogEntity entity = RouterTransLogConverter.convert(transLog);
        mapper.upsert(entity);
    }

    public void store(List<RouterTransLog> transLogs) {
        if (CollectionUtil.isNotEmpty(transLogs)) {
            List<RouterTransLogEntity> entities = RouterTransLogConverter.convert(transLogs);
            entities.stream().forEach(entity -> mapper.upsert(entity));
        }
    }

    public RouterTransLogEntity findByTransLogId(String transLogId) {
        return mapper.findByTransLogId(transLogId);
    }

    public List<RouterTransLogEntity> listByTransLogIds(List<String> transLogIds) {
        return mapper.createLambdaQuery().andIn(RouterTransLogEntity::getTransLogId, transLogIds).select();
    }

    public RouterTransLogEntity findByTradeIdAndAction(String tradeId, RouterAction action) {
        return mapper.findByTradeIdAndAction(tradeId, action);
    }

    public List<RouterTransLogEntity> listByTradeId(String tradeId) {
        return mapper.createLambdaQuery().andEq("trade_id", tradeId).select();

    }

    public RouterTransLogEntity findByChannelUniId(String channelUniId) {
        return mapper.findByChannelUniId(channelUniId);
    }

    public RouterTransLogEntity findByGatewayUniId(String gatewayUniId) {
        return mapper.findByGatewayUniId(gatewayUniId);
    }

    public List<RouterTransLogEntity> listByAction(RouterAction action) {
        return mapper.listByAction(action);
    }

}